<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of welcome
 *
 * @author Obi
 */
class Admin_produk extends MY_Controller {

    function __construct() {
        parent::__construct();

        $cek_admin = $this->session->userdata('cek_admin');
        if (!$cek_admin)
            redirect("admin");

        $this->mproduk = new M_produk();
        $this->mprodukdet = new M_produk_det();
        $this->mkatagori = new M_katagori();
    }

    function index() {

        $data['title'] = 'Data Produk';


        $data['list'] = $this->mproduk->order_by("tanggal", "desc")->get();

        $data['isi'] = 'isi';
        $this->load->view('template_admin', $data);
    }

    function view_det($id = NULL) {

        $data['title'] = 'Data Detail Produk';

        $data['list_katagori'] = $this->mkatagori->get();
        $data['produk'] = $this->mproduk->where("id = $id")->get();
        $data['produk_det'] = $this->mprodukdet->where("id_produk = $id")->get();

        $data['isi'] = 'isi_det';
        $this->load->view('template_admin', $data);
    }

    function tambah($error = NULL) {

        $data['title'] = 'Tambah Data Produk';
        $data['error'] = $error;

        $data['tmp_id'] = "";

        $data['list_katagori'] = $this->mkatagori->get();

        $data['isi'] = 'form';
        $this->load->view('template_admin', $data);
    }

    function delete($id) {
        $this->load->helper('file');
        delete_files('./uploads/' . $id . "/", TRUE);
        rmdir('./uploads/' . $id . "/");
        $this->mproduk->delete($id);
        redirect('admin_produk');
    }

    function simpan() {
        $path = $this->input->post('id');

        $cek_data = $this->mproduk->where("id = '$path'")->count();

        if ($cek_data == 0) {


            mkdir('./uploads/' . $path, 0777);

            $config['overwrite'] = false;
            $config['upload_path'] = './uploads/' . $path;
            $config['allowed_types'] = 'jpg|png|gif';
            $config['max_size'] = '100000';
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            $field1 = 'image1';
            $field2 = 'image2';
            $field3 = 'image3';

            $tmp_id = $this->input->post('tmp_id');
            if ($tmp_id == "") {
                if ($this->upload->do_upload($field1)) {
                    $uploaded = $this->upload->data();
                    $file1 = $uploaded['file_name'];

                    $config['image_library'] = 'gd2';
                    $config['source_image'] = "./uploads/" . $path . '/' . $file1;
                    $config['create_thumb'] = TRUE;
                    $config['maintain_ratio'] = TRUE;
                    $config['width'] = 200;
                    $config['height'] = 150;
                    $this->load->library('image_lib', $config);
                    $this->image_lib->initialize($config);
                    $this->image_lib->resize();
                    $this->image_lib->clear();
                }

                if ($this->upload->do_upload($field2)) {
                    $uploaded = $this->upload->data();
                    $file2 = $uploaded['file_name'];

                    $config['image_library'] = 'gd2';
                    $config['source_image'] = "./uploads/" . $path . '/' . $file2;
                    $config['create_thumb'] = TRUE;
                    $config['maintain_ratio'] = TRUE;
                    $config['width'] = 200;
                    $config['height'] = 150;
                    $this->load->library('image_lib', $config);
                    $this->image_lib->initialize($config);
                    $this->image_lib->resize();
                    $this->image_lib->clear();
                }

                if ($this->upload->do_upload($field3)) {
                    $uploaded = $this->upload->data();
                    $file3 = $uploaded['file_name'];
                    $config['image_library'] = 'gd2';
                    $config['source_image'] = "./uploads/" . $path . '/' . $file3;
                    $config['create_thumb'] = TRUE;
                    $config['maintain_ratio'] = TRUE;
                    $config['width'] = 200;
                    $config['height'] = 150;
                    $this->load->library('image_lib', $config);
                    $this->image_lib->initialize($config);
                    $this->image_lib->resize();
                    $this->image_lib->clear();
                }
                $data_produk = array(
                    'id' => $this->input->post('id'),
                    'nama_produk' => $this->input->post('nama_produk'),
                    'warna' => $this->input->post('warna'),
                    'harga' => $this->input->post('harga'),
                    'disk' => $this->input->post('disk'),
                    'stok' => $this->input->post('stok'),
                    'keterangan' => $this->input->post('keterangan'),
                    'id_katagori' => $this->input->post('id_katagori'),
                    'tanggal' => date('Y-m-d H:i:s'),
                    'image1' => $file1,
                    'image2' => $file2,
                    'image3' => $file3,
                    'status' => $this->input->post('status'),
                );

                $this->mproduk->insert($data_produk);
            }
            redirect('admin_produk');
        } else {
            $this->tambah("Kode Produk sudah ada !!!");
        }
    }

    function update() {

        $tmp_id = $this->input->post('tmp_id');


        $config['overwrite'] = false;
        $config['upload_path'] = './uploads/' . $tmp_id;
        $config['allowed_types'] = 'jpg|png|gif';
        $config['max_size'] = '100000';
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        $field1 = 'image1';
        $field2 = 'image2';
        $field3 = 'image3';

        if ($this->upload->do_upload($field1)) {
            $uploaded = $this->upload->data();
            $file1 = $uploaded['file_name'];

            $config['image_library'] = 'gd2';
            $config['source_image'] = "./uploads/" . $tmp_id . '/' . $file1;
            $config['create_thumb'] = TRUE;
            $config['maintain_ratio'] = TRUE;
            $config['width'] = 234;
            $config['height'] = 238;
            $this->load->library('image_lib', $config);
            $this->image_lib->initialize($config);
            $this->image_lib->resize();
            $this->image_lib->clear();
        }

        if ($this->upload->do_upload($field2)) {
            $uploaded = $this->upload->data();
            $file2 = $uploaded['file_name'];

            $config['image_library'] = 'gd2';
            $config['source_image'] = "./uploads/" . $tmp_id . '/' . $file2;
            $config['create_thumb'] = TRUE;
            $config['maintain_ratio'] = TRUE;
            $config['width'] = 180;
            $config['height'] = 200;
            $this->load->library('image_lib', $config);
            $this->image_lib->initialize($config);
            $this->image_lib->resize();
            $this->image_lib->clear();
        }

        if ($this->upload->do_upload($field3)) {
            $uploaded = $this->upload->data();
            $file3 = $uploaded['file_name'];
            $config['image_library'] = 'gd2';
            $config['source_image'] = "./uploads/" . $tmp_id . '/' . $file3;
            $config['create_thumb'] = TRUE;
            $config['maintain_ratio'] = TRUE;
            $config['width'] = 180;
            $config['height'] = 200;
            $this->load->library('image_lib', $config);
            $this->image_lib->initialize($config);
            $this->image_lib->resize();
            $this->image_lib->clear();
        }

        if (isset($file1)) {
            $data_produk1 = array(
                'image1' => $file1,
            );
            $this->mproduk->update($data_produk1, $tmp_id);
        }

        if (isset($file2)) {
            $data_produk2 = array(
                'image2' => $file2,
            );
            $this->mproduk->update($data_produk2, $tmp_id);
        }

        if (isset($file3)) {
            $data_produk3 = array(
                'image3' => $file3,
            );
            $this->mproduk->update($data_produk3, $tmp_id);
        }




        $data_produk = array(
            'nama_produk' => $this->input->post('nama_produk'),
            'warna' => $this->input->post('warna'),
            'harga' => $this->input->post('harga'),
            'disk' => $this->input->post('disk'),
            'stok' => $this->input->post('stok'),
            'keterangan' => $this->input->post('keterangan'),
            'id_katagori' => $this->input->post('id_katagori'),
            'tanggal' => date('Y-m-d H:i:s'),
            'status' => $this->input->post('status'),
        );
        $this->mproduk->update($data_produk, $tmp_id);

        redirect("admin_produk/view_det/" . $tmp_id);
    }

    function tambah_det($id_produk = NULL) {

        $data['title'] = 'Tambah Data Produk Detail';

        $data['id'] = "";
        $data['size_produk'] = "";
        $data['stok'] = "";
        $data['berat'] = "";
        $data['tmps_id'] = $id_produk;
        $data['status'] = "1";


        $data['isi'] = 'form_det';
        $this->load->view('template_admin', $data);
    }

    function edit_det($id = NULL) {

        $data['title'] = 'Tambah Data Produk Detail';

        $dta = $this->mprodukdet->where("id = $id")->get();

        $data['id'] = $dta->id;
        $data['size_produk'] = $dta->size_produk;
        $data['stok'] = $dta->stok;
        $data['berat'] = $dta->berat;
        $data['status'] = $dta->status;
        $data['tmps_id'] = $dta->id_produk;
        ;

        $data['isi'] = 'form_det';
        $this->load->view('template_admin', $data);
    }

    function simpan_det() {
        $id = $this->input->post('id');
        $data_produk = array(
            'size_produk' => $this->input->post('size_produk'),
            'stok' => $this->input->post('stok'),
            'berat' => $this->input->post('berat'),
            'status' => $this->input->post('status'),
            'id_produk' => $this->input->post('tmps_id'),
        );

        if ($id == "")
            $this->mprodukdet->insert($data_produk);
        else
            $this->mprodukdet->update($data_produk, $id);

        $this->view_det($this->input->post('tmps_id'));
    }

    function delete_det($id) {
        $this->mprodukdet->delete($id);
        redirect('admin_produk');
    }

    function delete_directory($dirname) {
        if (is_dir($dirname))
            $dir_handle = opendir($dirname);
        if (!$dir_handle)
            return false;
        while ($file = readdir($dir_handle)) {
            if ($file != "." && $file != "..") {
                if (!is_dir($dirname . "/" . $file))
                    unlink($dirname . "/" . $file);
                else
                    delete_directory($dirname . '/' . $file);
            }
        }
        closedir($dir_handle);
        rmdir($dirname);
    }

}

?>
